package com.ebt.m.customer.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.ebt.m.R;

import cn.carbswang.android.numberpickerview.library.NumberPickerView;

public class NumberPickerDialog extends Dialog implements View.OnClickListener,
		NumberPickerView.OnScrollListener, NumberPickerView.OnValueChangeListener{

	private static final String TAG = "picker";

	private TextView mTitleView;
	private TextView mPositiveView;
	private TextView mNegativeView;

	private NumberPickerView mNumberPickerView;
	private String[] mDisplayValues;
    private String mTitle;
    private int mPickedValue;
    private int mType;

    /**
     *
     * @param context
     * @param displayValues
     * @param pickedValue 取值范围[0 ~ displayValues.length - 1]
     */
	public NumberPickerDialog(Context context, String title, String[] displayValues, int pickedValue, int type) {
		super(context, R.style.dialog);
        mTitle = title;
        mDisplayValues = displayValues;
        mPickedValue = pickedValue;
        mType = type;
    }

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_number_picker);

		mNumberPickerView = (NumberPickerView) this.findViewById(R.id.picker);
		mNumberPickerView.setOnScrollListener(this);
		mNumberPickerView.setOnValueChangedListener(this);
        mNumberPickerView.refreshByNewDisplayedValues(mDisplayValues);
        mNumberPickerView.setValue(mPickedValue);

        mTitleView = (TextView) this.findViewById(R.id.tv_title);
        mTitleView.setText(mTitle);
        mPositiveView = (TextView) this.findViewById(R.id.tv_positive);
        mNegativeView = (TextView) this.findViewById(R.id.tv_negative);
        mPositiveView.setOnClickListener(this);
        mNegativeView.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.tv_positive:
				if (mSelectedListener != null) {
                    String[] content = mNumberPickerView.getDisplayedValues();
                    if (content != null) {
                        mSelectedListener.onSelected(
                                content[mNumberPickerView.getValue() - mNumberPickerView.getMinValue()],
                                mNumberPickerView.getValue(),
                                mType);
                    }
                }
				break;
            case R.id.tv_negative:
                break;
		}
        this.dismiss();
	}

	@Override
	public void onScrollStateChange(NumberPickerView view, int scrollState) {
		Log.d(TAG, "onScrollStateChange : " + scrollState);
	}

	@Override
	public void onValueChange(NumberPickerView picker, int oldVal, int newVal) {
		String[] content = picker.getDisplayedValues();
		if (content != null) {
			Log.d(TAG,"onValueChange content : " + content[newVal - picker.getMinValue()]);
		}
	}

    public interface SelectedListener{
        void onSelected(String content, int value, int type);
    }

    private SelectedListener mSelectedListener;
    public void setSelectedListener(SelectedListener listener){
        mSelectedListener = listener;
    }

}
